package com.gigi.service.manual;

import cn.hutool.core.util.StrUtil;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gigi.common.Constants;
import com.gigi.entity.GigiClassEntity;
import com.gigi.entity.UserEntity;
import com.gigi.enums.ErrorCodeEnum;
import com.gigi.enums.YesOrNoEnum;
import com.gigi.mapper.generated.UserMapper;
import com.gigi.model.ChangePicRequest;
import com.gigi.model.IdRequest;
import com.gigi.model.UserDO;
import com.gigi.model.UserLoginRequest;
import com.gigi.service.generated.GigiClassService;
import com.gigi.service.generated.UserService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;
import java.util.Objects;

@Service
public class UserManageService {

    private final UserService userService;
    private final UserMapper userMapper;
    private final GigiClassService gigiClassService;

    public UserManageService(UserService userService, UserMapper userMapper, GigiClassService gigiClassService) {
        this.userService = userService;
        this.userMapper = userMapper;
        this.gigiClassService = gigiClassService;
    }

    /**
     * 登录接口
     *
     * @param request
     * @return
     */
    public SingleResponse<Long> login(UserLoginRequest request) {
        LambdaQueryWrapper<UserEntity> wrapper = new LambdaQueryWrapper<UserEntity>()
                .eq(UserEntity::getPhone, request.getPhone())
                .eq(UserEntity::getPassword, request.getPwd())
                .eq(UserEntity::getFlag, YesOrNoEnum.YES.getCode());
        UserEntity entity = userService.getOne(wrapper, false);
        if (Objects.isNull(entity)) {
            return SingleResponse.buildFailure(ErrorCodeEnum.ACCOUNT_NOT_EXIST.getErrCode(),
                    ErrorCodeEnum.ACCOUNT_NOT_EXIST.getErrDesc());
        }
        return SingleResponse.of(entity.getId());
    }

    /**
     * 注册接口
     *
     * @param request
     * @return
     */
    public Response register(@RequestBody @Valid UserDO request) {
        LambdaQueryWrapper<UserEntity> wrapper = new LambdaQueryWrapper<UserEntity>()
                .eq(UserEntity::getPhone, request.getPhone())
                .eq(UserEntity::getFlag, YesOrNoEnum.YES.getCode());
        if (userService.count(wrapper) > 0) {
            return Response.buildFailure(ErrorCodeEnum.ALREADY_REGISTER.getErrCode(),
                    ErrorCodeEnum.ALREADY_REGISTER.getErrDesc());
        }
        UserEntity entity = new UserEntity();
        entity.setType(request.getType());
        entity.setPhone(request.getPhone());
        entity.setPassword(request.getPwd());
        entity.setName(request.getName());
        entity.setNo(request.getNo());
        entity.setClassid(request.getClassId());
        entity.setFlag(request.getFlag());
        entity.setPic(StrUtil.isNotBlank(request.getPic()) ? request.getPic() : Constants.ORI_PIC);
        if (!userService.save(entity)) {
            return Response.buildFailure(ErrorCodeEnum.SAVE_FAILED.getErrCode(),
                    ErrorCodeEnum.SAVE_FAILED.getErrDesc());
        }
        return Response.buildSuccess();
    }

    /**
     * 详情接口
     *
     * @param request
     * @return
     */
    public SingleResponse<UserDO> detail(IdRequest request) {
        UserEntity entity = userMapper.selectById(request.getId());
        if (Objects.isNull(entity)) {
            return SingleResponse.buildFailure(ErrorCodeEnum.NO_DATA.getErrCode(), ErrorCodeEnum.NO_DATA.getErrDesc());
        }

        UserDO userDO = new UserDO();
        if (YesOrNoEnum.YES.getCode().equals(entity.getType())) {
            GigiClassEntity classEntity = gigiClassService.getById(entity.getClassid());
            if (Objects.isNull(classEntity)) {
                return SingleResponse.buildFailure(ErrorCodeEnum.NO_DATA.getErrCode(), ErrorCodeEnum.NO_DATA.getErrDesc());
            }
            userDO.setClassName(classEntity.getName());
        }

        userDO.setId(entity.getId());
        userDO.setName(entity.getName());
        userDO.setClassId(entity.getClassid());
        userDO.setNo(entity.getNo());
        userDO.setType(entity.getType());
        userDO.setPhone(entity.getPhone());
        userDO.setPwd(entity.getPassword());
        userDO.setFlag(entity.getFlag());
        userDO.setPic(entity.getPic());
        return SingleResponse.of(userDO);
    }

    /**
     * 换头像接口
     *
     * @param request
     * @return
     */
    public Response changePic(ChangePicRequest request) {
        UserEntity entity = userMapper.selectById(request.getId());
        if (Objects.isNull(entity)) {
            return Response.buildFailure(ErrorCodeEnum.NO_DATA.getErrCode(), ErrorCodeEnum.NO_DATA.getErrDesc());
        }
        UserEntity target = new UserEntity();
        target.setId(entity.getId());
        target.setPic(StrUtil.isNotBlank(request.getPic()) ? request.getPic() : Constants.ORI_PIC);
        boolean isSuccess = userService.updateById(target);
        if (!isSuccess) {
            return Response.buildFailure(ErrorCodeEnum.EDIT_FAILED.getErrCode(), ErrorCodeEnum.EDIT_FAILED.getErrDesc());
        }
        return Response.buildSuccess();
    }

}
